Deploying a trained model in ML Engine

After training our CNN model, we can now deploy it to ML Engine and run our predictions on the cloud!

Deploy a version from your trained model


In [ ]:
%%bash
cd cifar10

MODEL_NAME="cifar10"
VERSION_NAME="v1"
JOB_DIR="gs://dost_deeplearning_cifar10/cifar10_train_1499931245" # Change this to your own

gcloud ml-engine models create $MODEL_NAME
gcloud ml-engine versions create \
  $VERSION_NAME \
  --model $MODEL_NAME \
  --origin $JOB_DIR/model

Predict with your deployed model

Let's try predicting with our deployed model! We've prepared a input json instance containing an image of a frog for testing.


In [ ]:
%%bash
cd cifar10

MODEL_NAME="cifar10"
VERSION_NAME="v1"

gcloud ml-engine predict \
  --model $MODEL_NAME \
  --version $VERSION_NAME \
  --json-instances predict_test.json

It should output 6 which is the label index for the frog class.

Emojify

Let's run a web application that will use our deployed model to "emojify" arbitrary images!

Install dependencies


In [ ]:
!pip install -r emojify/requirements.txt

Run server


In [ ]:
import os
import subprocess
import IPython
from google.datalab.utils import pick_unused_port

port = pick_unused_port()

# Config is reckoned from env vars
env = {
    'PROJECT_ID': 'dost-deeplearning', # Change this to your project id
    'MODEL_NAME': 'cifar10',
    'PORT': str(port),
}

args = ['python', 'emojify/emojify.py']
subprocess.Popen(args, env=env)
    
url = '/_proxy/%d/' % port
html = 'Running emojify! Click <a href="%s" target="_blank">here</a> to access it.' % url
IPython.display.display_html(html, raw=True)